/**
 * @license Copyright (c) 2014-2022, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
 */

'use strict';

/* eslint-env node */
const path = require('path');
const webpack = require('webpack');
const {bundler, styles} = require('@ckeditor/ckeditor5-dev-utils');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserWebpackPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const {CKEditorTranslationsPlugin} = require('@ckeditor/ckeditor5-dev-translations');

// 是否创建 Map 文件
let CreateSourceMap = false;

module.exports = {
    devtool: 'source-map',
    performance: {hints: false},
    entry: path.resolve(__dirname, 'src', 'ckeditor.js'),
    output: {
        library: 'ClassicEditor',
        path: path.resolve(__dirname, 'build'),
        filename: 'ckeditor.js',
        libraryTarget: 'umd',
        libraryExport: 'default'
    },
    optimization: {
        minimizer: [
            new TerserWebpackPlugin({
                extractComments: false,
                sourceMap: CreateSourceMap,
                terserOptions: {
                    output: {
                        comments: /^!/
                    }
                }
            })
        ]
    },
    plugins: [
        // 复制前端样式
        new CopyWebpackPlugin({
            patterns: [
                {from: path.join(__dirname, 'src', 'content.css'), to: path.join(__dirname, 'build')}
            ]
        }),
        // 分离样式文件
        new MiniCssExtractPlugin({filename: 'ckeditor.css'}),
        // 定入默认语言
        new CKEditorTranslationsPlugin({language: 'zh-cn', additionalLanguages: 'all'}),
        // 写入版本信息
        new webpack.BannerPlugin({raw: true, banner: bundler.getLicenseBanner()}),
        new webpack.BannerPlugin('For licensing, see https://gitee.com/zoujingli/think-admin-editor'),
    ],
    module: {
        rules: [
            {
                test: /\.svg$/,
                use: ['raw-loader']
            },
            {
                test: /\.css$/,
                use: [
                    {loader: MiniCssExtractPlugin.loader},
                    {loader: 'css-loader', options: {sourceMap: CreateSourceMap}},
                    {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: styles.getPostCssConfig({
                                minify: true,
                                themeImporter: {
                                    themePath: require.resolve('@ckeditor/ckeditor5-theme-lark')
                                },
                            })
                        }
                    },
                ]
            }
        ]
    }
};